package com.quectel.cms.controller.device;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.device.dto.DeviceOn3dDto;
import com.quectel.core.module.device.service.DeviceOn3dService;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import com.quectel.util.kit.PageFetchKit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2023/03/24 09:31
 */
@RestController
@RequestMapping("deviceOn3d")
@Tag(name = "DeviceOn3dController", description = "设备在3D地图点位信息相关api")
public class DeviceOn3dController extends BaseController {

    @DubboReference
    private DeviceOn3dService deviceOn3dService;

    /**
     * 查询该小区下所有在3d地图上已挂载的设备
     *
     * @param villageId
     * @return
     */
    @GetMapping("queryAll")
    @Operation(summary = "查询该小区下所有在3d地图上已挂载的设备")
    public Response<List<DeviceOn3dDto>> queryAll(
            @Parameter(description = "小区id") @RequestParam Long villageId
    ) {
        //查询列表数据
        Request request = Request.configParamsNotPage(Request.parseParams(httpServletRequest));

        List<DeviceOn3dDto> list = PageFetchKit.fetchAll(afterParams -> deviceOn3dService.queryList(afterParams), request.getParams());

        return Response.<List<DeviceOn3dDto>>ok().wrap(list.stream().filter(v -> v.getDeviceDto() != null && v.getDeviceDto().getDeviceCategory() != null)
                .collect(Collectors.toList()));
    }

    /**
     * 批量保存
     *
     * @param list
     * @return
     */
    @PostMapping("batchSave/{villageId}")
    @RequirePermissions(values = "deviceOn3d:edit")
    @Operation(summary = "批量新增")
    public Response<Object> batchSave(@PathVariable("villageId") Long villageId,
                                      @RequestBody List<DeviceOn3dDto> list) {
        deviceOn3dService.batchSave(villageId, list);
        return Response.ok();
    }


}
